package com.ai.bitcoin.bean;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.*;
import com.ai.bitcoin.serialize.BeanSerial;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONObject;

import java.lang.reflect.Field;
import java.util.List;

/**
 * @author Jason
 * @date 2018/4/4
 **/
@Setter
@Getter
public class BccAllInfo {
    public BccAllInfo(BccBasicInfo bccBasicInfo, BccDailyInfo bccDailyInfo, BccPriceInfo bccPriceInfo, List<BccInfoEdit> edits, List<BccIndexInfo> indexInfos, JSONObject other) {
        this.bccBasicInfo = bccBasicInfo;
        this.bccDailyInfo = bccDailyInfo;
        this.edits = edits;
        this.indexInfos = indexInfos;
        this.bccPriceInfo = bccPriceInfo;
        this.other = other;
    }

    private JSONObject jsonObject;
    public BccBasicInfo bccBasicInfo;
    public BccDailyInfo bccDailyInfo;
    public BccPriceInfo bccPriceInfo;
    List<BccInfoEdit> edits;
    List<BccIndexInfo> indexInfos;
    private JSONObject other;

    public JSONObject getJsonObject() {
        if (jsonObject == null || jsonObject.size() == 0) {
            jsonObject = new JSONObject();
            try {
                Field[] fields = this.getClass().getFields();//getDeclaredFields()获得某个类的所有申明的字段，即包括public、private和proteced，但是不包括父类的申明字段
                for (int i = 0; i < fields.length; i++) {
                    Object o = fields[i].get(this);
                    if (o != null)
                        jsonObject.putAll(BeanSerial.Bean2Json(o));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (null != edits && edits.size() > 0) {
            BccInfoEdit bccInfoEdit = null;
            for (int i = 0; i < edits.size(); i++) {
                bccInfoEdit = edits.get(i);
                jsonObject.put(bccInfoEdit.getFieldKey(), bccInfoEdit.getFieldValue());
            }
        }
        if (null != indexInfos && indexInfos.size() > 0) {
            BccIndexInfo bccIndexInfo = null;
            for (int i = 0; i < indexInfos.size(); i++) {
                bccIndexInfo = indexInfos.get(i);
                jsonObject.put(bccIndexInfo.getIndexTitle(), bccIndexInfo.getIndexValue());
            }
        }
        if (other != null)
            jsonObject.putAll(other);
        return jsonObject;
    }
}
